<template>
  <el-dialog
    title="初始密码修改"
    :close-on-click-modal="false"
    append-to-body
    v-model="is_show"
    :before-close="beforeClose"
    width="550px"
  >
    <el-form
      ref="updateFormRef"
      :model="updateForm"
      :rules="rules"
      label-width="100px"
    >
      <!-- <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          v-model="updateForm.oldPassword"
          placeholder="请输入旧密码"
          type="password"
          show-password
          size="large"
        />
      </el-form-item> -->
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model.trim="updateForm.newPassword"
          placeholder="请输入新密码"
          type="password"
          show-password
          size="large"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model.trim="updateForm.confirmPassword"
          placeholder="请确认新密码"
          type="password"
          show-password
          size="large"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="large"
          :loading="loading"
          @click="handleSubmit()"
          >保存</el-button
        >
        <el-button size="large" @click="beforeClose">关闭</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup name="changePwd">
import { updateUserPwd } from '@/api/system/user'
import { ref, reactive, computed, getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance()
const is_show = computed(() => {
  return true
})
const props = defineProps({
  oldPassword: {
    type: String,
    default: ''
  }
})
const loading = ref(false)
const updateForm = reactive({
  newPassword: undefined,
  confirmPassword: undefined
})
const { newPassword, confirmPassword } = toRefs(updateForm)
const validatePass = (rule, value, callback) => {
  //必须包含⼤⼩写字母、数字、特殊字符长度再9-16位之间
  var regex = /^(?![^a-zA-Z]+$)(?!\D+$)(?![a-zA-Z0-9]+$).{8,16}$/
  if (value === '') {
    callback(new Error('请输⼊密码'))
  } else if (value.length < 8 || value.length > 16) {
    callback(new Error('请输⼊8~16位密码'))
  } else if (!regex.test(value)) {
    callback(
      new Error('请输入包含英文字母大小写、数字和特殊符号的 8-16 位组合')
    )
  } else {
    callback()
  }
}

const equalToPassword = (rule, value, callback) => {
  if (updateForm.newPassword !== value) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}
const rules = ref({
  // oldPassword: [{ required: true, message: '旧密码不能为空', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '新密码不能为空', trigger: 'blur' },
    { required: true, validator: validatePass, trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    { required: true, validator: equalToPassword, trigger: 'blur' }
  ]
})

async function init() {}
init()
function beforeClose() {
  proxy.$emit('closeDialog')
}
function handleSubmit() {
  proxy.$refs.updateFormRef.validate(valid => {
    if (valid) {
      loading.value = true
      updateUserPwd(props.oldPassword, updateForm.newPassword).then(() => {
        loading.value = false
        proxy.$modal.msgSuccess('修改成功')
        beforeClose()
        location.reload()
      })
    }
  })
}
</script>
